package com.younio.local.compnent;

import com.younio.local.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @desc:
 * @author: lan     [1728209643@qq.com]
 * @date: 2018-08-20 15:05
 */
@Slf4j
@ControllerAdvice
public class ExceptionHandler {

    @ResponseBody
    @org.springframework.web.bind.annotation.ExceptionHandler(Exception.class)
    public Result handle(Exception e) {
        String errMsg = "系统繁忙";
        if (e instanceof RuntimeException) {
            errMsg = e.getLocalizedMessage();
        } else if (e instanceof MissingServletRequestParameterException) {
            MissingServletRequestParameterException parameterException = (MissingServletRequestParameterException) e;
            errMsg = "缺少参数" + parameterException.getParameterName();
        } else {
            e.printStackTrace();
        }
        log.error(errMsg);
        return Result.FAILED(errMsg);
    }

}
